﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ypn.common.csharp
{
    public partial class RTFTextBox : UserControl
    {
        public RTFTextBox()
        {
            InitializeComponent();
            dataUpdate = 0;
            InitializeControls();
        }

        #region Properties

        /// <summary>
        /// 获取自定义控件 RTFTextBox 内部的 RichTextBox
        /// </summary>
        public RichTextBox InnerRichTextBox
        {
            get
            {
                return this.richTextBox1;
            }
        }

        /// <summary>
        /// 获取和设置自定义控件 RTFTextBox 的 Text
        /// </summary>
        public override string Text
        {
            get
            {
                return richTextBox1.Text;
            }
            set
            {
                richTextBox1.Text = value;
            }
        }

        #endregion

        #region 自定义方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitializeControls()
        {
            BeginUpdate();

            //工具栏
            foreach (FontFamily family in FontFamily.Families)
            {
                toolStripComboBoxName.Items.Add(family.Name);
            }
            toolStripComboBoxName.SelectedItem = richTextBox1.Font.FontFamily.Name;
            Console.WriteLine("toolStripComboBoxName.SelectedItem = " + toolStripComboBoxName.SelectedItem);
            if (StringHelper.IsNull(toolStripComboBoxName.SelectedItem) && toolStripComboBoxName.Items.Count > 0)
            {
                toolStripComboBoxName.SelectedIndex = 0;
            }

            toolStripComboBoxSize.Items.AddRange(FontSize.All.ToArray());
            toolStripComboBoxSize.Text = richTextBox1.Font.Size.ToString();
            Console.WriteLine("richTextBox1.Font.Size = " + richTextBox1.Font.Size);
            Console.WriteLine("toolStripComboBoxSize.Text = " + toolStripComboBoxSize.Text);
            if (StringHelper.IsNull(toolStripComboBoxSize.Text) && toolStripComboBoxSize.Items.Count > 0)
            {
                toolStripComboBoxSize.SelectedIndex = 0;
            }
            
            EndUpdate();
        }

        /// <summary>
        /// 设置字体样式
        /// </summary>
        private void SetFont()
        {
            FontFamily fontFamily = new FontFamily(toolStripComboBoxName.Text);

            int fontSize = (StringHelper.IsNull(toolStripComboBoxSize.Text)) ? 12 : int.Parse(toolStripComboBoxSize.Text);

            FontStyle fontStyle = FontStyle.Regular;
            if (toolStripButtonBold.Checked)
            {
                fontStyle = FontStyle.Bold;
            }
            if (toolStripButtonItalic.Checked)
            {
                fontStyle = fontStyle | FontStyle.Italic;
            }
            if (toolStripButtonUnderline.Checked)
            {
                fontStyle = fontStyle | FontStyle.Underline;
            }

            richTextBox1.SelectionFont = new Font(fontFamily, fontSize, fontStyle);
        }

        #endregion

        #region Updating

        private int dataUpdate;
        private bool Updating
        {
            get
            {
                return dataUpdate != 0;
            }
        }

        private void BeginUpdate()
        {
            ++dataUpdate;
        }
        private void EndUpdate()
        {
            --dataUpdate;
        }

        #endregion

        #region 工具栏

        private void toolStripComboBoxName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            SetFont();
        }
        private void toolStripComboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }
            
            SetFont();
        }

        private void toolStripButtonBold_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            SetFont();
        }

        private void toolStripButtonItalic_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            SetFont();
        }

        private void toolStripButtonUnderline_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            SetFont();
        }

        private void toolStripButtonColor_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            ColorDialog dialog = new ColorDialog();
            dialog.Color = Color.FromArgb(0xff, 0xff, 0xff, 0xff);
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string color = dialog.Color.Name;
                if (!dialog.Color.IsNamedColor)
                {
                    color = "#" + color.Remove(0, 2);
                }
                richTextBox1.SelectionColor = ColorTranslator.FromHtml(color);
            }
        }

        private void toolStripButtonBullets_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            richTextBox1.SelectionBullet = true;
        }

        private void toolStripButtonOutdent_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            richTextBox1.SelectionIndent = richTextBox1.SelectionIndent + 10;
        }

        private void toolStripButtonIndent_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            richTextBox1.SelectionIndent = richTextBox1.SelectionIndent - 10;
        }

        private void toolStripButtonLeft_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void toolStripButtonCenter_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
        }

        private void toolStripButtonRight_Click(object sender, EventArgs e)
        {
            if (Updating)
            {
                return;
            }

            richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
        }

        #endregion

        #region richTextBox

        private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText); //实现内容中的网址单击后可以访问网址
        }

        #endregion

        #region Font Size
        private class FontSize
        {
            private static List<FontSize> allFontSize = null;
            public static List<FontSize> All
            {
                get
                {
                    if (allFontSize == null)
                    {
                        allFontSize = new List<FontSize>();
                        allFontSize.Add(new FontSize(5, 1));
                        allFontSize.Add(new FontSize(8, 2));
                        allFontSize.Add(new FontSize(9, 3));
                        allFontSize.Add(new FontSize(10, 4));
                        allFontSize.Add(new FontSize(11, 5));
                        allFontSize.Add(new FontSize(12, 6));
                        allFontSize.Add(new FontSize(14, 7));
                        allFontSize.Add(new FontSize(16, 8));
                        allFontSize.Add(new FontSize(18, 9));
                        allFontSize.Add(new FontSize(20, 10));
                        allFontSize.Add(new FontSize(22, 11));
                        allFontSize.Add(new FontSize(24, 12));
                        allFontSize.Add(new FontSize(26, 13));
                        allFontSize.Add(new FontSize(28, 14));
                        allFontSize.Add(new FontSize(36, 15));
                        allFontSize.Add(new FontSize(48, 16));
                        allFontSize.Add(new FontSize(72, 17));
                    }

                    return allFontSize;
                }
            }

            public static FontSize Find(int value)
            {
                if (value < 1)
                {
                    return All[0];
                }

                if (value > 7)
                {
                    return All[6];
                }

                return All[value - 1];
            }

            private FontSize(int display, int value)
            {
                displaySize = display;
                valueSize = value;
            }

            private int valueSize;
            public int Value
            {
                get
                {
                    return valueSize;
                }
            }

            private int displaySize;
            public int Display
            {
                get
                {
                    return displaySize;
                }
            }

            public override string ToString()
            {
                return displaySize.ToString();
            }
        }
        #endregion

        #region ToolStripComboBox
        private class ToolStripComboBoxEx : ToolStripComboBox
        {
            public override Size GetPreferredSize(Size constrainingSize)
            {
                Size size = base.GetPreferredSize(constrainingSize);
                size.Width = Math.Max(Width, 0x20);
                return size;
            }
        }
        #endregion

    }
}
